
(function () {
  var stateMap, elemMap, setElemMap, bindElemEvent, handleSignNow;

  stateMap = {
    lat: 0,
    lon: 0
  };

  /**********B 事件处理函数**********/
  handleSignNow = function () {
    if (Number(elemMap.phoneIpt.value).toString().length !== 11) {
      util.toast('请输入正确的手机号！');
      return false;
    }

    if (!elemMap.pwdIpt.value) {
      util.toast('请输入密码！');
      return false;
    }

    //禁止多次点击提交登录
    elemMap.signNow.style.pointerEvents = 'none';
    util.addFetchLoading();

    $.ajax({
      url: util.hostUrl + '/v1/userInfo/login.html',
      type: 'post',
      data: {
        header: JSON.stringify({
          reqKey: util.reqKey
        }),
        param: JSON.stringify({
          app: util.app,
          loginType: 2,
          phone: elemMap.phoneIpt.value,
          pass: elemMap.pwdIpt.value,
          platform: 3,
          channel: localStorage.jqyChannel,
          longitude: stateMap.lon,
          latitude: stateMap.lat
        })
      },
      success: function (res) {
        /*************S 记录点击登录************/
        var t = JSON.parse(localStorage.TJBasic);
        t.CurrentPage = '登录';
        t.time = (new Date()).valueOf();
        t.behavior = '点击立即登录';
        t.sfLogin = localStorage.jqyPhone ? '是' : '否';
        t.loginType = '密码登录';
        t.state = res.result.msg;

        var arr = JSON.parse(localStorage.TJArray);
        arr.push(t);
        localStorage.TJArray = JSON.stringify(arr);
        /*************E 记录点击登录************/

        if (res.result.code == 1) {
          localStorage.jqyToken  = res.result.token;
          localStorage.jqyCookie = res.result.cookie;
          localStorage.jqyUserId = res.response.userId;
          localStorage.jqyPhone  = res.response.phone;

          //完善统计信息
          var temp = JSON.parse(localStorage.TJBasic);
          temp.phone = res.response.phone;
          temp.userId = res.response.userId;
          localStorage.TJBasic = JSON.stringify(temp);

          var urlMap = util.splitUrl(location.search);
          if (urlMap.to && urlMap.to == 'proDetail') {     //产品详情
            location.replace('./proDetail.html?id=' + urlMap.id);
          } else if (urlMap.to && urlMap.to == 'reply') {  //申请记录
            location.replace('./replyList.html');
          } else if (urlMap.to && urlMap.to == 'setPwd') { //设置密码
            location.replace('./setPwd.html');
          } else if (urlMap.to && urlMap.to == 'me') {  //个人中心
            location.replace('./me.html');
          } else {
            location.replace('./index.html');
          }
        } else if (res.result.code == 9) {
          util.toast('该用户还未注册喔！');
        } else if (res.result.code == 10) {
          util.toast('密码输入错误！');
        } else {
          util.toast('Oops,发生错误！');
        }
      },
      error: function () {
        util.toast('Oops,网络有问题！');
      },
      complete: function () {
        elemMap.signNow.style.pointerEvents = 'auto';
        util.removeFetchLoading();
      }
    });
  };
  /**********E 事件处理函数**********/

  bindElemEvent = function () {
    elemMap.phoneIpt.addEventListener('keyup', function () {
      util.showOrHideClearBtn(this, elemMap.iptClear);
    });
    elemMap.iptClear.addEventListener('click', function () {
      util.clearIpt(elemMap.phoneIpt, this);
    });
    elemMap.pwdIpt.addEventListener('keyup', function () {
      util.showOrHidePreviewBtn(this, elemMap.iptEye);
    });
    elemMap.iptEye.addEventListener('click', function () {
      util.changePwdState(elemMap.pwdIpt, this);
    });
    elemMap.signNow.addEventListener('click', handleSignNow);
  };

  setElemMap = function () {
    elemMap = {
      phoneIpt    : document.querySelector('#phoneIpt'),
      iptClear    : document.querySelector('.ipt-clear'),
      pwdIpt      : document.querySelector('#pwdIpt'),
      iptEye      : document.querySelector('.ipt-eye'),
      signNow     : document.querySelector('.sign-now')
     };
  };

  setElemMap();
  bindElemEvent();
  util.setPageHeight();
  util.getLocation(function (position) {
    stateMap.lat = position.coords.latitude;
    stateMap.lon = position.coords.longitude;
  });
  
}());
